package com.ys.code.generator.util;

import java.io.File;
import java.io.StringWriter;
import java.io.Writer;

import org.springframework.stereotype.Component;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

@Component
public class TemplateUtil {
	protected Configuration getConfiguration() throws Exception {
		Configuration cfg = new Configuration();
		String basePath = this.getClass().getResource("/").getPath();
		File file = new File(basePath + "com/ys/code/generator/template");
		cfg.setDirectoryForTemplateLoading(file);
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		return cfg;
	}

	public String getText(String templateFile, Object params) throws Exception {
		Template template = getConfiguration().getTemplate(templateFile, "UTF-8");

		Writer out = new StringWriter();

		template.process(params, out);

		return out.toString();
	}
	
	public static void main(String[] args) throws Exception {
		//TemplateUtil.getConfiguration();
	}
}